<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\User;

use App\Controller\AppBaseController;
use App\Model\Party;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\WxApiAuth;

#[Controller(prefix: 'user')]
#[Middleware(WxApiAuth::class)]
class PartyController extends AppBaseController
{
    /**
     * 支部列表
     * @return object
     */
    #[RequestMapping(path:'partyList',methods: 'get')]
    public function partyList(): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            $where['status'] = 1;
            $list = Party::where($where)->selectRaw('id,name')->get();
            $next = 0;
            return $this->reMsg->re(1,'success',['list' => $list, 'next' => $next]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 支部信息
     * @param int $party_id
     * @return object
     */
    #[RequestMapping(path:'partyInfo/{party_id}',methods: 'get')]
    public function partyInfo(int $party_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            $info = Party::where('id',$party_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);
            $info->author_status = 0;
            if($_user->party_id==$info->id && in_array($_user->user_type,[8,9])){
                $info->author_status = 1;
            }
            return $this->reMsg->re(1,'success',$info);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

    /**
     * 支部信息更新
     * @param int $party_id
     * @return object
     */
    #[RequestMapping(path:'upPartyInfo/{party_id}',methods: 'post,get')]
    public function upPartyInfo(int $party_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            if($party_id!=$_user->party_id || !in_array($_user->user_type,[8,9])) return $this->reMsg->re(10009);

            $data = $this->request->all();

            //虚拟数据
//            if(empty($data)){
//                $data['party_secretary'] = '张小奇';
//                $data['party_committee'] = '张小奇';
//                $data['party_number'] = 50;
//                $data['party_public_number'] = 100;
//                $data['transition_time'] = '2024-10-01 10:00:00';
//                $data['content'] = '正式的修改';
//                $data['images'] = ['/fqxinyi23/test/xyMessage1689216632369.png'];
//            }
            //虚拟数据

            if( empty($data['party_secretary'])
                || empty($data['party_committee'])
                || empty($data['party_number']) || !is_numeric($data['party_number'])
                || empty($data['party_public_number']) || !is_numeric($data['party_public_number'])
                || empty($data['transition_time'])
                || empty($data['content'])
                || empty($data['images'])
            ){
                return $this->reMsg->re(10007);
            }

            $info = Party::where('id',$party_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);
            $info->party_secretary = $data['party_secretary'];
            $info->party_committee = $data['party_committee'];
            $info->party_number = $data['party_number'];
            $info->party_public_number = $data['party_public_number'];
            $info->transition_time = $data['transition_time'];
            $info->content = $data['content'];
            $info->images = $data['images'];
            $info->save();
            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

}